package com.clickpaas.materialsprocurement.dao.Impl;

import com.bizcloud.ipaas.t0ba0ba6afb694026b6b4e4e048af2b43.d20210202135416.codegen.PurchaselineApi;
import com.bizcloud.ipaas.t0ba0ba6afb694026b6b4e4e048af2b43.d20210202135416.model.*;
import com.clickpaas.materialsprocurement.bean.MateralsDTOBean;
import com.clickpaas.materialsprocurement.constant.ConstantFile;
import com.clickpaas.materialsprocurement.dao.PurchaseLineDao;
import com.clickpaas.materialsprocurement.entity.PurchaseOrderLineEntity;
import com.clickpaas.materialsprocurement.utils.SpringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Repository
public class PurchaseLineDaoImpl implements PurchaseLineDao {

    @Resource
    private MateralsDTOBean materalsDtoBean;
    @Resource
    private PurchaselineApi purchaselineApi;

    private List<PurchaseLineDTOResponse> queryPurchaseLineInfo(PurchaseLineDTO purchaseLineDTO){
        DataResponseListPurchaseLineDTO purchaseLineUsingPOST = purchaselineApi.findPurchaseLineUsingPOST(purchaseLineDTO);
        if(null == purchaseLineUsingPOST
                || null == purchaseLineUsingPOST.getData()
                || purchaseLineUsingPOST.getData().size()<=0
                || !"SUCCESS".equals(purchaseLineUsingPOST.getMessage())){
            log.info("当前查询的数据：【{}】",purchaseLineUsingPOST);
            return null;
        }
        return purchaseLineUsingPOST.getData();
    }

    /**
     * 根据条件查询采购订单行对象数据
     *
     * @param purchaseOrderLineEntity
     */
    @Override
    public List<PurchaseLineDTOResponse> getPurchaseLineByCondtion(PurchaseOrderLineEntity purchaseOrderLineEntity) {
        PurchaseLineDTO purchaseLineDTO = materalsDtoBean.purchaseLineDTO();
        try {
            SpringUtil.copyPropertiesIgnoreNull(purchaseOrderLineEntity,purchaseLineDTO);
        }catch (Exception e){
            log.info("对象转换失败！转换的对象是：【{}】",purchaseOrderLineEntity);
        }
        return queryPurchaseLineInfo(purchaseLineDTO);
    }

    /**
     * 采购订单行对象数据入库
     *
     * @param purchaseOrderLineEntity
     */
    @Override
    public Boolean insertPurchaseLine(PurchaseOrderLineEntity purchaseOrderLineEntity) {
        PurchaseLineDTOUpdate purchaseLineDTOUpdate = materalsDtoBean.purchaseLineDTOUpdate();
        try {
            SpringUtil.copyPropertiesIgnoreNull(purchaseOrderLineEntity,purchaseLineDTOUpdate);
        }catch (Exception e){
            log.info("对象转换失败！转换的对象是：【{}】",purchaseOrderLineEntity);
        }
        SaveOrUpdatePurchaseLineDTO saveOrUpdatePurchaseLineDTO = new SaveOrUpdatePurchaseLineDTO();
        saveOrUpdatePurchaseLineDTO.setQuery(materalsDtoBean.purchaseLineDTOQuery());
        saveOrUpdatePurchaseLineDTO.setUpdate(purchaseLineDTOUpdate);
        return insertData(saveOrUpdatePurchaseLineDTO);
    }

    private Boolean insertData(SaveOrUpdatePurchaseLineDTO saveOrUpdatePurchaseLineDTO){
        try {
            PurchaseLineSaveOrUpdateDataResponseObject dataResponseString = purchaselineApi.saveOrUpdatePurchaseLineUsingPOST(saveOrUpdatePurchaseLineDTO);
            if (null != dataResponseString
                    && ConstantFile._MESSAGE.equals(dataResponseString.getMessage())
                    && null != dataResponseString.getData())
                return true;
        }catch (Exception e){
            log.info("采购订单入库失败：【{}】",e);
        }
        return false;
    }
}
